/*
 * Copyright (c) 2021 hpmicro
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */
#include "hpm_soc.h"
#include "hpm_ppor_drv.h"
#include "hpm_l1c_drv.h"
#include "hpm_gpio_drv.h"
#include "hpm_romapi.h"
#include "board.h"
#include "flash_map.h"
#include "ota_api.h"
#include "boot_user.h"

//pbutn
void bootuser_init_gpio_pins(void)
{
    uint32_t pad_ctl = IOC_PAD_PAD_CTL_PE_SET(1) | IOC_PAD_PAD_CTL_PS_SET(1);
    HPM_IOC->PAD[IOC_PAD_PZ02].FUNC_CTL = IOC_PZ02_FUNC_CTL_GPIO_Z_02;
    HPM_IOC->PAD[IOC_PAD_PZ02].PAD_CTL = pad_ctl;
    /* PZ port IO needs to configure BIOC as well */
    HPM_BIOC->PAD[IOC_PAD_PZ02].FUNC_CTL = IOC_PZ02_FUNC_CTL_SOC_PZ_02;
}


void bootuser_board_init(void)
{
    board_init();
    bootuser_init_gpio_pins();
}

bool bootuser_board_enter_bootloader(void)
{
    uint32_t counter = 0;
    gpio_set_pin_input(HPM_GPIO0, GPIO_DI_GPIOZ, 2);

    for (uint8_t i = 0; i < 10; i++)
    {
        if (0 == gpio_read_pin(HPM_GPIO0, GPIO_DI_GPIOZ, 2))
        {
            counter++;
        }
        board_delay_ms(10);
    }
    if (counter > 9)
    {
        return true;
    }
    return false;
}

int bootuser_check_enter_mode(void)
{
    if (bootuser_board_enter_bootloader())
    {
        return -1;
    }

    if (ota_board_app_valid(FLASH_USER_APP1_ADDR, BOOT_USER_APP1_SIGNATURE))
    {
        return 0;
    }

    if (ota_board_app_valid(FLASH_USER_APP2_ADDR, BOOT_USER_APP2_SIGNATURE))
    {
        return 1;
    }

    return -1;
}

int bootuser_verfiy_goto_app(bool verfiy_enable)
{
    user_fota_header_t* fota_header = NULL;
    if (ota_board_app_valid(FLASH_USER_APP1_ADDR, BOOT_USER_APP2_SIGNATURE))//判断 第二区间内是否有固件
    {
        if (ota_board_app_valid(FLASH_USER_APP2_ADDR, BOOT_USER_APP2_SIGNATURE))
        {
            if(verfiy_enable)
            {
                fota_header = (user_fota_header_t*)FLASH_APP2_HEADER_ADDR;
                printf("fota_header -> len  = %d\r\n",fota_header->len);
                if(!ota_board_auto_verify(FLASH_USER_APP2_ADDR, fota_header))
                {
                    printf("BAD! firmware image verify fail!\r\n");
                    return 0;//检测到有固件，但固件完整性验证失败
                }
                if(get_board_app_version(FLASH_USER_APP1_ADDR)<get_board_app_version(FLASH_USER_APP2_ADDR))
                {
                    printf("firmware verfiy success!\r\n");
                    return 1;//检测到固件，且固件完整性验证成功，且版本正确
                }else{
                    printf("firmware version so low\r\n");
                    return 0;//版本太老
                }  
            }
        }
        else{
            printf("No firmware upgrade detected\r\n");//没有检测到有固件需要升级
            return -1;
        }
    }else{
        if (ota_board_app_valid(FLASH_USER_APP2_ADDR, BOOT_USER_APP2_SIGNATURE))
        {
            if(verfiy_enable)
            {
                fota_header = (user_fota_header_t*)FLASH_APP2_HEADER_ADDR;
                printf("fota_header -> len  = %d\r\n",fota_header->len);
                if(!ota_board_auto_verify(FLASH_USER_APP2_ADDR, fota_header))
                {
                    printf("BAD! firmware image verify fail!\r\n");
                    return 0;//检测到有固件，但固件完整性验证失败
                }else{
                    printf("no app in slot1 and find a app in slot2\r\n");
                    return 1;//检测到固件，且固件完整性验证成功
                }
            }
        }else{
            printf("Blank storage, start receiving program!\r\n");
            return 2;//flash 的第二第三区间完全空白，直接进入接收模式。
        }
    }
   
}
